package com.gxuwz.hmr.project.system.agriculture.service.impl;

import java.util.List;

import com.gxuwz.hmr.project.system.agriculture.domain.Agriculture;
import com.gxuwz.hmr.project.system.agriculture.domain.Region;
import com.gxuwz.hmr.project.system.agriculture.mapper.AgricultureMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.project.system.agriculture.domain.Region;
import com.ruoyi.project.system.agriculture.mapper.AgricultureMapper;
import com.ruoyi.project.system.agriculture.domain.Agriculture;
import com.ruoyi.project.system.agriculture.service.IAgricultureService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 农合经办信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-05
 */
@Service
public class AgricultureServiceImpl implements IAgricultureService 
{
    @Autowired
    private AgricultureMapper agricultureMapper;

    /**
     * 查询农合经办信息
     * 
     * @param agrId 农合经办信息ID
     * @return 农合经办信息
     */
    @Override
    public Agriculture selectAgricultureById(Long agrId)
    {
        return agricultureMapper.selectAgricultureById(agrId);
    }

    /**
     * 查询农合经办信息列表
     * 
     * @param agriculture 农合经办信息
     * @return 农合经办信息
     */
    @Override
    public List<Agriculture> selectAgricultureList(Agriculture agriculture)
    {
        return agricultureMapper.selectAgricultureList(agriculture);
    }

    /**
     * 新增农合经办信息
     * 
     * @param agriculture 农合经办信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAgriculture(Agriculture agriculture)
    {
        agriculture.setCreateTime(DateUtils.getNowDate());
        int rows = agricultureMapper.insertAgriculture(agriculture);
        insertRegion(agriculture);
        return rows;
    }

    /**
     * 修改农合经办信息
     * 
     * @param agriculture 农合经办信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAgriculture(Agriculture agriculture)
    {
        agriculture.setUpdateTime(DateUtils.getNowDate());
        agricultureMapper.deleteRegionByRegionId(agriculture.getAgrId());
        insertRegion(agriculture);
        return agricultureMapper.updateAgriculture(agriculture);
    }

    /**
     * 删除农合经办信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAgricultureByIds(String ids)
    {
        agricultureMapper.deleteRegionByRegionIds(Convert.toStrArray(ids));
        return agricultureMapper.deleteAgricultureByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除农合经办信息信息
     * 
     * @param agrId 农合经办信息ID
     * @return 结果
     */
    @Override
    public int deleteAgricultureById(Long agrId)
    {
        agricultureMapper.deleteRegionByRegionId(agrId);
        return agricultureMapper.deleteAgricultureById(agrId);
    }

    /**
     * 新增行政区域信息信息
     * 
     * @param agriculture 农合经办信息对象
     */
    public void insertRegion(Agriculture agriculture)
    {
        List<Region> regionList = agriculture.getRegionList();
        Long agrId = agriculture.getAgrId();
        if (StringUtils.isNotNull(regionList))
        {
            List<Region> list = new ArrayList<Region>();
            for (Region region : regionList)
            {
                region.setRegionId(agrId);
                list.add(region);
            }
            if (list.size() > 0)
            {
                agricultureMapper.batchRegion(list);
            }
        }
    }
}
